//
//  SLCATransitionController.m
//  Objective-C
//
//  Created by S_Line on 2018/7/2.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCATransitionController.h"

@interface SLCATransitionController ()

@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, strong) NSArray *images;

@end

@implementation SLCATransitionController

- (void)initNavBar {
    self.navTitle = @"过渡动画";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.imageView = imageView;
    [self.view addSubview:imageView];
    
    self.images = @[
                    [UIImage imageNamed:@"nav_comment"],
                    [UIImage imageNamed:@"nav_share"],
                    [UIImage imageNamed:@"nav_menu"],
                    [UIImage imageNamed:@"nav_back"]
                    ];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 40)];
    [button setTitle:@"Switch Image" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *diyTransitionButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 250, 200, 40)];
    [diyTransitionButton setTitle:@"Perform Transition" forState:UIControlStateNormal];
    [diyTransitionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [diyTransitionButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [diyTransitionButton addTarget:self action:@selector(diyTransitionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:diyTransitionButton];
}

- (void)buttonClick:(UIButton *)sender {
//    // set up crossfade transition
//    CATransition *transition = [CATransition animation];
//    transition.type = kCATransitionReveal;
//    transition.subtype = kCATransitionFromLeft;
//    // apply transition to imageview backing layer
//    [self.imageView.layer addAnimation:transition forKey:nil];
//    // cycle to next image
//    UIImage *currentImage = self.imageView.image;
//    NSUInteger index = [self.images indexOfObject:currentImage];
//    index = (index + 1) % [self.images count];
//    self.imageView.image = self.images[index];
    
    [UIView transitionWithView:self.imageView duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        //cycle to next image
        UIImage *currentImage = self.imageView.image;
        NSUInteger index = [self.images indexOfObject:currentImage];
        index = (index + 1) % [self.images count];
        self.imageView.image = self.images[index];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)diyTransitionButtonClick:(UIButton *)sender {
    // preserve the current view snapshot
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *coverImage = UIGraphicsGetImageFromCurrentImageContext();
    // insert snap shot view in front of this one
    UIImageView *coverView = [[UIImageView alloc] initWithImage:coverImage];
    coverView.frame = self.view.bounds;
    [self.view addSubview:coverView];
    // update the view (we'll simply randomize the layer background color)
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    // perform animation (anything you like)
    [UIView animateWithDuration:1.0 animations:^{
        // scale, rotate, and fade the view
        CGAffineTransform transform = CGAffineTransformMakeScale(0.01, 0.01);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        coverView.transform = transform;
        coverView.alpha = 0.0;
    } completion:^(BOOL finished) {
        // remove the cover view now we're finished with it
        [coverView removeFromSuperview];
    }];
}

@end
